﻿using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
using YiJingFramework.Annotating.Zhouyi;
using YiJingFramework.EntityRelations.GuaCharacters.Extensions;
using YiJingFramework.EntityRelations.GuaDerivations.Extensions;
using YiJingFramework.PrimitiveTypes.GuaWithFixedCount;

Console.OutputEncoding = Encoding.Unicode;

using var client = new HttpClient();
var zhouyi = await client.GetFromJsonAsync<ZhouyiStore>(
    "https://yueyinqiu.github.io/my-yijing-annotation-stores/975345ca/2023-08-02-1.json");
Debug.Assert(zhouyi is not null);

var trigrams = new[]
{
    GuaTrigram.Parse("111"),
    GuaTrigram.Parse("001"),
    GuaTrigram.Parse("010"),
    GuaTrigram.Parse("100"),
    GuaTrigram.Parse("000"),
    GuaTrigram.Parse("110"),
    GuaTrigram.Parse("101"),
    GuaTrigram.Parse("011")
};

var paddingChar = '　';
foreach(var trigram in trigrams)
{
    Console.WriteLine($"{zhouyi.GetTrigram(trigram).Name}{trigram.ToUnicodeChar()}宫：");

    var hexagram = new GuaHexagram(trigram.Concat(trigram));
    Console.Write($"本卦" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(0);
    Console.Write($"一世" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(1);
    Console.Write($"二世" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(2);
    Console.Write($"三世" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(3);
    Console.Write($"四世" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(4);
    Console.Write($"五世" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(3);
    Console.Write($"游魂" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}"
        .PadRight(4, paddingChar));

    hexagram = hexagram.ChangeYaos(0, 1, 2);
    Console.Write($"归魂" +
        $"{zhouyi.GetHexagram(hexagram).Name}{hexagram.ToUnicodeChar()}");

    Console.WriteLine();
    Console.WriteLine();
}
